﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Net.Sz.Framework.Collections.Concurrent
{

    [Serializable]
    public class ConcurrentHashSet<T> : IEnumerable<T>, IEnumerable
    {
        System.Collections.Concurrent.ConcurrentDictionary<T, Boolean> sets;

        public ConcurrentHashSet() { sets = new System.Collections.Concurrent.ConcurrentDictionary<T, bool>(); }

        public ConcurrentHashSet(int concurrencyLevel, int capacity) { sets = new System.Collections.Concurrent.ConcurrentDictionary<T, bool>(concurrencyLevel, capacity); }

        public IEnumerator GetEnumerator() { return sets.Keys.GetEnumerator(); }

        public bool Add(T item)
        {
            if (sets.ContainsKey(item))
                return false;
            return sets.TryAdd(item, true);
        }


        public void Clear() { sets.Clear(); }

        public bool Contains(T item) { return sets.ContainsKey(item); }

        public int Count { get { return sets.Count; } }

        public bool IsReadOnly { get { return false; } }

        public bool Remove(T item)
        {
            bool bb;
            return sets.TryRemove(item, out bb);
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator() { return sets.Keys.GetEnumerator(); }
    }
}
